# @Time    : 2018/12/24 21:07
# @Author  : heyin
# mktime和localtime互为转换
import time


def timestamp2datetime(timestamp):
    # 时间戳转日期时间格式
    # 34592400   北京时间1971-02-05 17:00:00
    # timestamp = 1541687400  # 2018-11-08 22:30:00
    time_array = time.localtime(timestamp)
    datetime = time.strftime("%Y-%m-%d %H:%M:%S", time_array)  # 字符串
    return datetime


def datetime2timestamp(datetime):
    # 日期时间格式转时间戳
    # datetime = '2018-11-08 22:30:00'
    timestamp = int(time.mktime(time.strptime(datetime, '%Y-%m-%d %H:%M:%S')))
    return timestamp


def timestamp2date(timestamp):
    if timestamp < 0:  # 小于1970.1.1的时间不清楚如何处理
        timestamp = 0
    time_array = time.localtime(timestamp)
    date = time.strftime("%Y-%m-%d", time_array)  # 字符串
    return date


def simple_ma_avg(stock_data, ma_list=[20, 60, 120, 200, 250]):
    """计算简单移动平均线"""
    # ma_list = [20, 60, 200]  # 计算多少天的均线
    # if not file_path.endswith('/'):
    #     file_path = file_path + '/'
    # stock_data = pd.read_csv('%s%s' % (file_path, file_name), parse_dates=[1])
    for ma in ma_list:
        # stock_data['MA_' + str(ma)] = pd.rolling(stock_data['close'], ma)
        stock_data['ma_' + str(ma)] = stock_data['close'].rolling(ma).mean().round(2)
    # print(stock_data)
    return stock_data
    # stock_data.to_csv('%sma_%s' % (file_path, file_name), index=False)
